<?php
    require_once('page_base_jectly.php');

	/*
     * sub classes of page_base_jectly will include only the changes from index_2;
	 */
	class project extends page_base_jectly {

        protected $contents_html='<div class=\'center\'>This is a project page.</div>';
        protected $id;//project_id
        public $project;//TableProject //it should be public to be used in template
		public $project_bid;
		public $projectbid;
		public $bid;
		public $request;
        //lists
        public $list_bid;//QDataRepeater
        public $list_request_cancel;//QDataRepeater
        public $list_request_complete;//QDataRepeater
        public $list_escrow;//QDataRepeater

        

        function __construct() {
            parent::__construct();
            $this->bool_main_menu_on=true;
            $this->bool_main_logo_on=true;
            $this->bool_search_tab_on=false;
            $this->bool_breadcrumb_on=false;
            $this->bool_main_3boxes_on=false;
            $this->bool_search_result_within_page_on=false;
            $this->bool_account_menu_on=false;
            $this->bool_contents_area_on=true;
        }

        protected function Form_Create() {

            //parent::Form_Create();

            $this->check_query_string();

            $this->panel_search_tab_Create();
            $this->panel_menubar_Create();

            if($this->bool_logined())
            {
                $this->pnl_mebubar->hide_all_controls();
            }

            if(isset($this->selected_category_index_string) && is_numeric($this->selected_category_index_string))
            {
                $this->set_index_listbox_search_project(intval($this->selected_category_index_string));
            }

            $this->load_project();

            //
            $this->create_list();
		}

        protected function check_query_string()
        {
            //check menubar mode
            $this->mode_string_page=QApplication::QueryString('p');

            if(!empty($this->mode_string_page))
            {
                if($this->mode_string_page ==='0')
                {
                    $this->bool_mode_login=true;
                    $this->bool_mode_register=false;
                    $this->bool_mode_forgot=false;
                }
                else if($this->mode_string_page ==='1')
                {
                    $this->bool_mode_login=false;
                    $this->bool_mode_register=true;
                    $this->bool_mode_forgot=false;
                }
                else if($this->mode_string_page ==='2')
                {
                    $this->bool_mode_login=false;
                    $this->bool_mode_register=false;
                    $this->bool_mode_forgot=true;
                }
            }
            else
            {
                $this->bool_mode_login=true;
                $this->bool_mode_register=false;
                $this->bool_mode_forgot=false;
            }

            $this->category_string=QApplication::QueryString('c');//category_id
            $this->selected_category_index_string=QApplication::QueryString('i');//category_list_index

            if(is_numeric(QApplication::QueryString('id')))
            {
                $this->id=QApplication::QueryString('id');//project_id
            }
        }

        protected function load_project()
        {
            if(isset($this->id) && is_numeric($this->id))
            {
               	$this->project=TableProject::LoadById($this->id);
                $this->projectbid=TableProjectBid::bid_id_for_the_project_for_buyer($this->project->UserId,$this->project->Id);
                $this->project_bid=TableProjectBid::LoadById($this->projectbid);
                $this->request=TableProjectRequestCancel::load_request_id_for_project($this->project->Id);
              
                if(isset($this->project))
                {
                    $this->contents_html='project loaded-['.  $this->project->Id.'] user_id=['.$this->project->UserId.'] bid_id=['.  $this->project_bid.']';

                    //$this->add_html_contents($this->project->Description);

                    //test japanese characters
                    //$this->project->Os='ウィンドウズ';
                    //$this->project->Save();
                }
                else
                {
                    $this->contents_html='project not found id=['.$this->id.']';
                    $this->id=null;
                    $this->project=null;
                }
            }
        }
        

        protected function create_list()
        {
            $this->list_bid=new QDataRepeater($this);
            //$this->list_bid->Paginator = new QPaginator($this);
            //$this->list_bid->PaginatorAlternate = new QPaginator($this);
            //$this->list_bid->ItemsPerPage = 1000;
            $this->list_bid->UseAjax = false;
            $this->list_bid->Template = __INCLUDES__ . '/tpl_qrepeater/list_bid_in_project.tpl.php';//_in_project is the convention to mean the list in proejct.php
            $this->list_bid->SetDataBinder('bind_list_bid');
            
            $this->list_request_cancel= new QDataRepeater($this);
            $this->list_request_cancel->UseAjax=false;
            $this->list_request_cancel->Template= __INCLUDES__.'/tpl_qrepeater/request_cancel_in_project.tpl.php';
            $this->list_request_cancel->SetDataBinder('bind_list_request_cancel');
            
             $this->list_request_complete= new QDataRepeater($this);
            $this->list_request_complete->UseAjax=false;
            $this->list_request_complete->Template= __INCLUDES__.'/tpl_qrepeater/request_complete_in_project.tpl.php';
            $this->list_request_complete->SetDataBinder('bind_list_request_complete');
            
            
        }

        protected function bind_list_bid()
        {
            if(isset($this->project))
            {
                 $array_=$this->project->load_bids_all_for_the_project_order_by_bid_date();

                if(!isset($array_))
                {
                    $array_=array();
                }

                //$this->list_bid->TotalItemCount=sizeof($array_);
                $this->list_bid->DataSource=$array_;
            }
        }
        
        protected function bind_list_request_cancel()
        {
            if(isset($this->project))
            {
                 $array_=$this->project->load_requests_cancel_all_for_the_project_order_by_request_date();

                if(!isset($array_))
                {
                    $array_=array();
                }

                //$this->list_bid->TotalItemCount=sizeof($array_);
                $this->list_request_cancel->DataSource=$array_;
            }
        }
           protected function bind_list_request_complete()
        {
            if(isset($this->project))
            {
                 $array_=$this->project->load_requests_complete_all_for_the_project_order_by_request_date();

                if(!isset($array_))
                {
                    $array_=array();
                }

                //$this->list_bid->TotalItemCount=sizeof($array_);
                $this->list_request_complete->DataSource=$array_;
            }
        }

        protected function add_html_contents($text_)
        {
            $this->contents_html=$this->contents_html.'<br />'.$text_;
        }

        protected function bool_project_loaded()
        {
            return isset($this->project);
        }
        
        protected function bool_status()
	{	
		
		
		if($this->project_bid->bool_active())
		{return true;}
		
		return false;

	}

        protected function bool_my_project()
        {
            if(isset($this->project))
            {
                if($this->bool_logined())
                {
                    if($this->project->UserId==$this->UserId)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        protected function bool_won_project()
        {
            if(isset($this->project))
            {
                if($this->bool_logined())
                {
                    if($this->project->WonUserId==$this->UserId)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        protected function bool_open_project()
        {
            if(isset($this->project))
            {
                if($this->project->Status==0 || $this->project->Status==1 || $this->project->Status==2)
                {
                    return true;
                }
            }
            return false;
        }

        protected function bool_after_bid_project()
        {
            if(isset($this->project))
            {
                if($this->project->Status==2)
                {
                    return true;
                }
            }
            return false;
        }

      
 /* protected function bool_after_picked_project()
        {
            if(isset($this->project))
            {
                if($this->project->Status==3)
                {
                    return true;
                }
            }
            return false;
        }*/
        protected function bool_starting_project()
        {
            if(isset($this->project))
            {
                if($this->project->Status==4 || $this->project->Status==5)
                {
                    return true;
                }
            }
            return false;
        }

        protected function bool_canceled_project()
        {
            if(isset($this->project))
            {
                if($this->project->Status==6 || $this->project->Status==7)
                {
                    return true;
                }
            }
            return false;
        }

        protected function bool_canceled_and_reviewed_project()
        {
            if(isset($this->project))
            {
                if($this->project->Status==7)
                {
                    return true;
                }
            }
            return false;
        }

        protected function bool_completed_project()
        {
            if(isset($this->project))
            {
                if($this->project->Status==8 || $this->project->Status==9)
                {
                    return true;
                }
            }
            return false;
        }

        protected function bool_completed_and_reviewed_project()
        {
            if(isset($this->project))
            {
                if($this->project->Status==9)
                {
                    return true;
                }
            }
            return false;
        }

        protected function bool_failed_project()
        {
            if(isset($this->project))
            {
                if($this->project->Status==10 || $this->project->Status==11)
                {
                    return true;
                }
            }
            return false;
        }

        protected function bool_failed_and_reviewed_project()
        {
            if(isset($this->project))
            {
                if($this->project->Status==11)
                {
                    return true;
                }
            }
            return false;
        }

        protected function bool_expired_project()
        {
            if(isset($this->project))
            {
                if($this->project->Status==12 || $this->project->Status==13 || $this->project->Status==14 || $this->project->Status==15)
                {
                    return true;
                }
            }
            return false;
        }

        protected function bool_canceled_and_expired_project()
        {
            if(isset($this->project))
            {
                if($this->project->Status==12)
                {
                    return true;
                }
            }
            return false;
        }

        protected function bool_completed_and_expired_project()
        {
            if(isset($this->project))
            {
                if($this->project->Status==13)
                {
                    return true;
                }
            }
            return false;
        }

        protected function bool_failed_and_expired_project()
        {
            if(isset($this->project))
            {
                if($this->project->Status==14)
                {
                    return true;
                }
            }
            return false;
        }

        protected function bool_no_winner_and_expired_project()
        {
            if(isset($this->project))
            {
                if($this->project->Status==15)
                {
                    return true;
                }
            }
            return false;
        }

        public function render_contents_area() {

            //$this->contents_html=$this->contents_html.$this->bool_my_project();

            if($this->bool_contents_area_on)
            {
                if($this->bool_my_project())echo('this is my project<br />');
                if(!$this->bool_my_project())echo('this is not my project<br />');
                if($this->bool_open_project())echo('this is an open project<br />');
                if(!$this->bool_open_project())echo('this is not an open project<br />');
                echo($this->contents_html);
            }
        }

        public function render_development_links() {

            //$this->contents_html=$this->contents_html.$this->bool_my_project();

            if($this->bool_project_loaded() && $this->bool_logined())
            {
                echo('--before starting--');
                echo('<br />');

                echo($this->project->get_textlink_for_project_message_popup());
                echo($this->project->get_textlink_to_bid_popup());

                
                if (($this->project_bid) ) {

                	echo($this->project_bid->get_textlink_to_cancel_bid_popup());
                	echo($this->project_bid->get_textlink_to_edit_bid_popup());
                	
                                
                
                }else{
                	
                	echo($this->project->get_textlink_to_cancel_bid_popup());
                	echo($this->project->get_textlink_to_edit_bid_popup());
                
                
                }
                
              
           		echo($this->project->get_textlink_to_select_bid_popup());
                echo($this->project->get_textlink_to_manage_won_popup());
                echo('<br />');
                echo('--after started--');
                echo('<br />');
                echo('for buyer:');
               
              	echo($this->project->get_textlink_to_request_cancel_popup());//will called only by the buyer
              		
                echo($this->project->get_textlink_to_accept_complete_popup());//will be called only by the buyer
                echo($this->project->get_textlink_to_decline_complete_popup());//will be called only by the buyer
                echo($this->project->get_textlink_to_retract_cancel_popup());//will called only by the buyer
                echo('<br />');
                echo('for provider:');
                echo($this->project->get_textlink_to_request_complete_popup());//will be called only by the provider
                echo($this->project->get_textlink_to_accept_cancel_popup());//will called only by the provider
                echo($this->project->get_textlink_to_decline_cancel_popup());//will called only by the provider
                echo($this->project->get_textlink_to_retract_complete_popup());//will called only by the provider
                echo('<br />');
                echo('--after [closed(cancelled) or failed or completed]--');
                echo('<br />');
                echo($this->project->get_textlink_to_new_review_popup());
            }
        }

        public function render_development_lists() {

            //$this->contents_html=$this->contents_html.$this->bool_my_project();

            if($this->bool_project_loaded() && $this->bool_logined())
            {
                echo('--bidding list & action--');
                echo('<br />');
                $this->list_bid->Render();
                echo('<br />');
                echo('--request to cancel list & action--');
                echo('<br />');
                  $this->list_request_cancel->Render();
                echo('<br />');
                echo('--request to complete list & action--');
                echo('<br />');
                $this->list_request_complete->Render();
                echo('<br />');
                echo('--escrows list for the project(not required by now)--');
                echo('<br />');
            }
        }
         public function render_projects_lists() {

            //$this->contents_html=$this->contents_html.$this->bool_my_project();

            if($this->bool_project_loaded() && $this->bool_logined())
            {
               
                echo($this->project->get_textlink_to_bid_popup());
                if($this->project_bid)
                {
                echo($this->project_bid->get_textlink_to_retract_bid_popup());
                }else 
                {
                	  echo($this->project->get_textlink_to_retract_bid_popup());
                }
                	
                	echo($this->project->get_textlink_to_accept_won_popup());
                
                echo($this->project->get_textlink_to_retract_won_popup());
                echo($this->project->get_textlink_to_bookmark_project_popup());
                echo($this->project->get_textlink_to_report_violation_popup());
            }
        }

        

	}

    project::Run('project','project.tpl.php');

?>
